package com.stedu.mall.orderservice.controller;

import com.github.pagehelper.PageInfo;
import com.stedu.mall.common.bean.Order;
import com.stedu.mall.common.bean.OrderVo;
import com.stedu.mall.common.bean.RespBean;
import com.stedu.mall.common.exception.SteduException;
import com.stedu.mall.common.service.OrderService;
import com.stedu.mall.common.uitl.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @PostMapping("/insert")
    public RespBean insert(@RequestBody OrderVo orderVo, @RequestHeader("token") String token) throws SteduException {
        //解析token获取用户Id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");

        orderVo.setUserId(userId);
        orderService.insert(orderVo);
        return RespBean.ok("添加成功", orderVo);
    }
    @DeleteMapping("/delete")
    public RespBean delete(String id){
        orderService.delete(id);
        return RespBean.ok("删除成功");
    }
    @GetMapping("/{id}")
    public RespBean selectById(@PathVariable("id") String id){
        Order order = orderService.selectById(id);
        return RespBean.ok("",order);
    }
    @GetMapping("/name/{id}")
    public RespBean selectNameById(@PathVariable("id") String id){
        Order order = orderService.selectById(id);
        String collect = order.getOrderDetailList()
                .stream()
                .map(item -> item.getGoods())
                .map(item -> item.getName())
                .collect(Collectors.joining("、"));
        return RespBean.ok("",collect);
    }
    @GetMapping("/selectAll")
    public RespBean selectAll(Order condition,Integer pageNum,Integer pageSize){
        pageNum = pageNum == null?1:pageNum;
        pageSize = pageSize == null ? 5:pageSize;
        PageInfo<Order> pageInfo = orderService.selectAll(condition,pageNum,pageSize);
        return RespBean.ok("查询成功",pageInfo);
    }

    @GetMapping("/selectByUserId")
    public RespBean selectByUserId(Integer pageNum, Integer pageSize, @RequestHeader("token") String token) {
        //解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");

        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 5 : pageSize;
        PageInfo<Order> pageInfo = orderService.selectByUserId(userId, pageNum, pageSize);
        return RespBean.ok("查询成功", pageInfo);
    }

    @GetMapping("/select/{id}")
    public RespBean selectById1(@PathVariable("id") String id, @RequestHeader("token") String token) throws SteduException {
        //解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");
        Order order = orderService.selectById1(id, userId);
        return RespBean.ok("根据id查询订单成功", order);
    }

    @PostMapping("/pay")
    public RespBean pay(@RequestBody OrderVo orderVo, @RequestHeader("token") String token) throws SteduException {
        //解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");

        orderVo.setUserId(userId);
        orderService.pay(orderVo);
        return RespBean.ok("支付成功");
    }

    @PutMapping("/harvest")
    public RespBean harvest(@RequestBody Order order) {
        order.setStatus(3);
        orderService.update(order);
        return RespBean.ok("已收货~~，祝您生活愉快");
    }

    @PutMapping("/send")
    public RespBean send(@RequestBody Order order) {
        order.setStatus(2);
        orderService.update(order);
        return RespBean.ok("发货成功");
    }
}
